import 'package:flutter/material.dart';
import 'h_colors.dart';
import 'h_text_styles.dart';
import 'h_dimens.dart';

/// Miaoyi UI框架主题系统
///
/// 提供对颜色、文本样式和尺寸的统一访问
class HTheme {
  /// 默认构造函数
  ///
  /// 需要颜色、文本样式和尺寸参数
  const HTheme({
    required this.colors,
    required this.textStyles,
    required this.dimens,
  });

  /// 颜色系统
  final HColors colors;

  /// 文本样式系统
  final HTextStyles textStyles;

  /// 尺寸系统
  final HDimens dimens;

  /// 创建亮色主题
  factory HTheme.light() {
    final colors = HColors.light();
    return HTheme(
      colors: colors,
      textStyles: HTextStyles(colors: colors),
      dimens: const HDimens(),
    );
  }

  /// 创建暗色主题
  factory HTheme.dark() {
    final colors = HColors.dark();
    return HTheme(
      colors: colors,
      textStyles: HTextStyles(colors: colors),
      dimens: const HDimens(),
    );
  }

  /// 根据亮暗模式创建对应主题
  factory HTheme.fromBrightness(Brightness brightness) {
    return brightness == Brightness.light
        ? HTheme.light()
        : HTheme.dark();
  }

  /// 获取当前主题
  static HTheme of(BuildContext context) {
    final inheritedTheme = context.dependOnInheritedWidgetOfExactType<_InheritedHTheme>();
    assert(inheritedTheme != null, 'No HTheme found in context');
    return inheritedTheme!.theme;
  }

  /// 创建Material主题数据
  ThemeData toThemeData() {
    final brightness = colors.background.computeLuminance() > 0.5
        ? Brightness.light
        : Brightness.dark;

    return ThemeData(
      primaryColor: colors.primary,
      scaffoldBackgroundColor: colors.background,
      cardColor: colors.cardBackground,
      dividerColor: colors.divider,
      disabledColor: colors.disabledBackground,
      colorScheme: colors.toColorScheme(brightness),
      textTheme: TextTheme(
        displayLarge: textStyles.h1,
        displayMedium: textStyles.h2,
        displaySmall: textStyles.h3,
        headlineMedium: textStyles.h4,
        headlineSmall: textStyles.h5,
        titleLarge: textStyles.h3,
        titleMedium: textStyles.emphasis,
        titleSmall: textStyles.cardTitle,
        bodyLarge: textStyles.body,
        bodyMedium: textStyles.body,
        bodySmall: textStyles.bodySmall,
        labelLarge: textStyles.body,
        labelMedium: textStyles.disabled,
        labelSmall: textStyles.secondary,
      ),
      buttonTheme: ButtonThemeData(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(dimens.borderRadius),
        ),
        padding: EdgeInsets.symmetric(
          horizontal: dimens.buttonPaddingHorizontalNormal,
          vertical: dimens.buttonPaddingVerticalNormal,
        ),
      ),
      inputDecorationTheme: InputDecorationTheme(
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(dimens.borderRadius),
          borderSide: BorderSide(
            color: colors.divider,
            width: dimens.borderWidth,
          ),
        ),
        contentPadding: EdgeInsets.symmetric(
          horizontal: dimens.inputPaddingHorizontal,
          vertical: dimens.inputPaddingVertical,
        ),
        labelStyle: textStyles.formLabel,
        hintStyle: textStyles.secondary,
        errorStyle: textStyles.formError,
      ),
      cardTheme: CardTheme(
        color: colors.cardBackground,
        elevation: dimens.cardElevation,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(dimens.borderRadius),
        ),
        margin: EdgeInsets.all(dimens.spacingM),
      ),
      dividerTheme: DividerThemeData(
        color: colors.divider,
        thickness: dimens.borderWidth,
        space: dimens.spacingM,
      ),
      dialogTheme: DialogTheme(
        backgroundColor: colors.cardBackground,
        elevation: dimens.dialogElevation,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(dimens.borderRadiusMedium),
        ),
      ),
      iconTheme: IconThemeData(
        color: colors.text,
        size: dimens.iconSizeNormal,
      ),
      tooltipTheme: TooltipThemeData(
        decoration: BoxDecoration(
          color: colors.text.withOpacity(0.9),
          borderRadius: BorderRadius.circular(dimens.borderRadius),
        ),
        textStyle: textStyles.body.copyWith(color: colors.cardBackground),
        padding: EdgeInsets.symmetric(
          horizontal: dimens.spacingM,
          vertical: dimens.spacingS,
        ),
      ),
      appBarTheme: AppBarTheme(
        backgroundColor: colors.primary,
        foregroundColor: Colors.white,
        elevation: dimens.cardElevation,
      ),
      snackBarTheme: SnackBarThemeData(
        backgroundColor: colors.cardBackground,
        contentTextStyle: textStyles.body,
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(dimens.borderRadiusMedium),
        ),
      ),
      chipTheme: ChipThemeData(
        backgroundColor: colors.surface,
        disabledColor: colors.disabledBackground,
        selectedColor: colors.primary,
        secondarySelectedColor: colors.primary,
        padding: EdgeInsets.symmetric(horizontal: dimens.spacingM),
        labelStyle: textStyles.body,
        secondaryLabelStyle: textStyles.body.copyWith(color: Colors.white),
        brightness: brightness,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(dimens.borderRadiusMedium),
          side: BorderSide(
            color: colors.divider,
            width: dimens.borderWidth,
          ),
        ),
      ),
      useMaterial3: true,
    );
  }

  /// 复制主题并更新指定值
  HTheme copyWith({
    HColors? colors,
    HTextStyles? textStyles,
    HDimens? dimens,
  }) {
    if (colors == null && textStyles == null && dimens == null) {
      return this;
    }

    final newColors = colors ?? this.colors;
    return HTheme(
      colors: newColors,
      textStyles: textStyles ?? HTextStyles(colors: newColors),
      dimens: dimens ?? this.dimens,
    );
  }
}

/// 主题提供者
///
/// 将HTheme提供给Widget树
class HThemeProvider extends StatelessWidget {
  /// 创建主题提供者
  ///
  /// [theme]为要提供的主题
  /// [themeMode]为主题模式，决定使用亮色还是暗色主题
  /// [child]为子Widget
  const HThemeProvider({
    super.key,
    this.theme,
    this.themeMode = ThemeMode.light,
    required this.child,
  });

  /// 主题，如果为null则根据[themeMode]创建主题
  final HTheme? theme;

  /// 主题模式
  final ThemeMode themeMode;

  /// 子Widget
  final Widget child;

  @override
  Widget build(BuildContext context) {
    final effectiveTheme = theme ?? (themeMode == ThemeMode.dark ? HTheme.dark() : HTheme.light());
    
    return _InheritedHTheme(
      theme: effectiveTheme,
      child: Theme(
        data: effectiveTheme.toThemeData(),
        child: child,
      ),
    );
  }
}

/// 继承小部件，用于在Widget树中传递HTheme
class _InheritedHTheme extends InheritedWidget {
  const _InheritedHTheme({
    required this.theme,
    required super.child,
  });

  final HTheme theme;

  @override
  bool updateShouldNotify(_InheritedHTheme old) => theme != old.theme;
} 